// pages/user/commission-list/index.js
const app = getApp()
const api = require('../../../api/user')
import { initLoadMoreMixin } from '../../../utils/list-load-more'

const TYPE_TEXT = {
  1: '订单',
  2: '推荐',
  3: '版本推荐',
  4: '版本代理',
  5: '用户推荐人',
  6: '用户推荐人上级',
  7: '用户代理'
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabItems: [{
      title: '全部',
      status: ''
    }, {
      title: '已确认',
      status: 1
    }, {
      title: '待确认',
      status: 0
    }],
    shopId: app.getUser().id,
    confirmCommission: 0,
    total: 0,
    activeIndex: 0,

    typeShow: false,
    typeName: '订单',
    typeIndex: 1,
    typeColumns: Object.values(TYPE_TEXT),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    initLoadMoreMixin.call(this)
    app.requestUrl({url: api.COMMISSION_TOTAL, data: {memberId: this.data.shopId, isGrant: 1}}).then(res => {
      this.setData({confirmCommission: res.data.data.toFixed(2)})
    })
    app.requestUrl({url: api.COMMISSION_TOTAL, data: {memberId: this.data.shopId}}).then(res => {
      this.setData({total: res.data.data.toFixed(2)})
    })
    this.resetInit()
  },

  initData () {
    const status = this.data.tabItems[this.data.activeIndex].status
    return app.requestUrl({
      url: api.COMMISSION_LOG,
      data: {
        mid: this.data.shopId,
        type: this.data.typeIndex,
        isGrant: status,
        offset: (this.data.pages.currPage - 1) * this.data.pages.perPage,
        limit: this.data.pages.perPage
      }
    }).then(res => {
      const {
        total,
        rows,
      } = res.data.data
      let orders = this.data.items

      rows.forEach(item => {
        let order = orders[item.orderId]
        if (!order) {
          order = {
            id: item.orderId,
            sn: item.orderSn,
            createTime: item.createTime,
            status: item.orderStatus,
            statusText: item.orderStatusName,
            // userName: item.realname || item.nickname,
            userName: item.name,
            totalCommission: 0,
            goods: []
          }

          orders.push(order)
        }
        order.totalCommission += item.commission
        order.goods.push({
          id: item.orderProductId,
          title: item.productName,
          optionname: item.optionName,
          thumb: item.thumbSquare || item.thumb,
          total: item.total,
          isGrant: item.isGrant,
          commission: item.commission,
          marketprice: item.marketPrice,
          unit: item.unit,
          specTitle: item.specTitle,
        })
      })

      this.setData({items: orders.filter(item => !!item)})

      return {
        pageCount: rows.length,
        totalCount: total
      }
    })
  },
  handleTabClick (e) {
    console.log(e)
    this.setData({activeIndex: e.detail.index})

    this.resetInit()
  },

  typeConfirm(event){
    this.setData({
      typeIndex: event.detail.index + 1,
      typeName: event.detail.value,
    })
    this.resetInit();
    this.typeMonitor();
  },
  typeMonitor () {
    let _this = this
    _this.setData({
      typeShow: !_this.data.typeShow
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
